﻿using System.Windows.Controls;
using Controllers;

namespace Views.Moduler
{
    public partial class Hardware : UserControl
    {
        #region Fields

        private UserControl _currentControl;
        private UserControl _uc;

        #endregion

        #region Creation

        public Hardware(Controller controller)
        {
            InitializeComponent();

            // gør filen tilgængelig i XAML delen
            DataContext = this;

            TabDiverse = new Diverse(controller);
            TabComputere = new Computere(controller);
            TabMobiler = new Mobiler(controller);
        }

        #endregion

        #region Properties

        public Computere TabComputere { get; set; }
        public Diverse TabDiverse { get; set; }
        public Mobiler TabMobiler { get; set; }

        #endregion

        #region Tilstandsstyring

        public void OpdaterSide()
        {
            if (_currentControl != null)
            {
                var diverse = _currentControl as Diverse;
                if (diverse != null)
                {
                    TabDiverse.OpdaterSide();
                }

                var computere = _currentControl as Computere;
                if (computere != null)
                {
                    TabComputere.OpdaterSide();
                }

                var mobiler = _currentControl as Mobiler;
                if (mobiler != null)
                {
                    TabMobiler.OpdaterSide();
                }
            }
        }

        private void HardwareTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _uc = ((TabItem) HardwareTabControl.SelectedItem).Content as UserControl;
            OpdaterListView();
        }

        public void OpdaterListView()
        {
            if (_uc != _currentControl)
            {
                _currentControl = _uc;
                var diverse = _uc as Diverse;
                if (diverse != null)
                {
                    TabDiverse.OpdaterSide();
                }

                var computere = _uc as Computere;
                if (computere != null)
                {
                    TabComputere.OpdaterSide();
                }

                var mobiler = _uc as Mobiler;
                if (mobiler != null)
                {
                    TabMobiler.OpdaterSide();
                }
            }
        }

        #endregion
    }
}
